/*
* ============================================================================
* 功能描述 ：串口命令层
* 作者       ：ggls
* 创建日期 ：2024.04.10
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/

#include "main.h"

static uart_buf_s uart_buf;
static void _uart_data_handle(void)
{
	char  th;
	char  ch;
	if(uart_buf.recv_buf[0] == 0x55)
	{
		if(uart_buf.recv_buf[1] == 0xaa)
		{
			if(uart_buf.recv_len > 7)
			{
				th = uart_buf.recv_buf[6]*256+uart_buf.recv_buf[7]; // 计算传输命令字节数，判断校验位，数据长度是否足够
				uart_buf.cmd_len = th+9;
			
			
				if(uart_buf.recv_len == uart_buf.cmd_len)
				{
					ch = specialSumCheck(uart_buf.recv_buf, uart_buf.recv_len -1);					// 校验数据
					if(ch == uart_buf.recv_buf[uart_buf.recv_len -1])
					{
						int i;
						for(i = 0; i < uart_buf.recv_len; i++)
						{
							uart_buf.cmd_buf[i] = uart_buf.recv_buf[i];
						}
						uart_buf.recv_hand_flag = true;
						uart_buf.recv_len = 0;
					}
					else
					{
						uart_buf.recv_len = 0;
					}
				}
			}
			
		}
	}
	else if (uart_buf.recv_buf[0] != 0x55)
		uart_buf.recv_len = 0;
}


void uart_cmd_rx_cplt_callback(void)
{
	uart_buf.recv_buf[uart_buf.recv_len] = RCREG;
	uart_buf.recv_len++;
	RCIF = 0;
	if (uart_buf.recv_len >12)
		uart_buf.recv_len = 0;
	
	_uart_data_handle();
}



void Zigbee_Cmd_handle(void)
{
	if(uart_buf.recv_hand_flag == true)
	{
		
		
		uart_buf.recv_hand_flag = false;
		
		if(uart_buf.cmd_buf[4] == DEVICE_MANAGE) 
		{
			if(uart_buf.cmd_buf[5] == zigbee_ready)
			{
				zigbee_app.zigbee_ready_flag = 1;
				Zigbee_wake_up();
				DelayXms(15);
				Hly_Packet_device_manage(zigbee_ready_back, 1, 0, 3);
				Zigbee_sleep();
			}
			else if(uart_buf.cmd_buf[5] == read_device_Endpoint)
			{

			}
			else if(uart_buf.cmd_buf[5] == read_device_type)
				{}
			else if(uart_buf.cmd_buf[5] == read_device_attribute)
				{}
			else if(uart_buf.cmd_buf[5] == modify_device_attribute)
				{}
			else if(uart_buf.cmd_buf[5] == request_device_Report_attr)
				{}
			else if(uart_buf.cmd_buf[5] == mcu_ready_reply)
				{}
			else if(uart_buf.cmd_buf[5] == mcu_set_Endpoint_reply)	
			{

			}
			else if(uart_buf.cmd_buf[5] == mcu_set_device_type_reply)
				{}
			else if(uart_buf.cmd_buf[5] == report_device_attribute_reply)
				{}
			else if(uart_buf.cmd_buf[5] == request_appoint_property_reply)
				{}
			else if(uart_buf.cmd_buf[5] == start_zigbee_protocol_reply)
				{}
			else if(uart_buf.cmd_buf[5] == read_device_Endpoint)
				{}
			else if(uart_buf.cmd_buf[5] == read_device_Endpoint)
				{}
			else
				{}
		}
		else if(uart_buf.cmd_buf[4] == NETWORK_MANAGE) 
		{
			if(uart_buf.cmd_buf[5] == notify_network_status)
			{
				zigbee_app.network_status = uart_buf.cmd_buf[8];
			}
			else if(uart_buf.cmd_buf[5] == query_network_status)
			{

				zigbee_app.network_status = uart_buf.cmd_buf[9];
			}
			else if(uart_buf.cmd_buf[5] == query_network_status)
			{
				zigbee_app.network_status = 0;

			}
			else
				{}
		}
			

	}
		
}





